﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace XtremBiking.Web.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        #region LogIn POST
        [HttpPost]
        public ActionResult LogIn(XtremBiking.Web.Models.Usuarios.LogInUser model)
        {
            String cookie;
            try
            {

                Tuple<String, XtremBiking.Logica.DTO.Usuario> result = XtremBiking.Logica.Usuarios.LogIn(model.Email, model.Password);

                switch (result.Item1)
                {
                    case "LoginIncorrecto":
                        TempData["Error"] = XtremBiking.Recursos.Controladores.Home.IncorrectLogin;
                        //return View();
                        return RedirectToAction("Index","Home");
                    case "PrimerLogin":
                        //if user is the first time to login, then the system redirecto to first login action
                        //create an authentication cookie only with de data necessary to realize first login password change.
                        cookie = result.Item2.IdUsuario.ToString() + "#" + result.Item2.Nombre + " " +result.Item2.Apellidos + "#" + ((int)result.Item2.Perfil).ToString() + "#" + result.Item2.Perfil.ToString() + "#" + result.Item2.Nivel.ToString() + "#" + result.Item2.FechaUltimoLogin.ToShortDateString();
                        FormsAuthentication.SetAuthCookie(cookie, false);
                        return RedirectToAction("FirstLogin", "Usuarios");
                    case "Suspendido":
                        TempData["Error"] = XtremBiking.Recursos.Controladores.Home.BlockedUser;
                        return RedirectToAction("Index", "Home");
                    case "LoginSatisfactorio":
                        cookie = result.Item2.IdUsuario.ToString() + "#" + result.Item2.Nombre + " " +result.Item2.Apellidos + "#" + ((int)result.Item2.Perfil).ToString() + "#" + result.Item2.Perfil.ToString() + "#" + result.Item2.Nivel.ToString() + "#" + result.Item2.FechaUltimoLogin.ToShortDateString();
                        FormsAuthentication.SetAuthCookie(cookie, false);
                        return RedirectToAction("Index","Home");
                }
                return RedirectToAction("Index","Home");
            }
            catch (Exception ex)
            {
                //TODO: Auditar el ex.Message
                //TempData["Message"] = Phoenix.Resources.BackOffice.Controllers.Home.IncorrectLogin;
                ModelState.AddModelError("", ex.Message);
            }
            return View(model);
        }
        #endregion
        #region LogOut
        /// <summary>
        /// Method responsible for logout the user 
        /// </summary>
        /// <returns>Redirect to the Index action</returns>
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
        #endregion
    }
}
